package com.example.myapplication;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "scoreCollectDB.db";   //定义数据库名称
    private static final String TABLE_NAME_STUDENT = "student";  //定义表名称
    private static final String CREATE_TABLE_SQL_STUDENT = "create table " + TABLE_NAME_STUDENT
            + " (_id integer primary key autoincrement, name text, number text, gender text, score text)";  //创建表的SQL语句


    public MySQLiteOpenHelper(@Nullable Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SQL_STUDENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long insertData(StudentBean student){

        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", student.getName());
        values.put("number", student.getNumber());
        values.put("gender", student.getGender());
        values.put("score", student.getScore());
        return db.insert(TABLE_NAME_STUDENT, null, values);
    }

    public int deleteDataFromDbByName(String name){
        SQLiteDatabase db = getWritableDatabase();
//        return db.delete(TABLE_NAME_STUDENT, "name like ?", new String[]{name});
        return db.delete(TABLE_NAME_STUDENT, "name=?", new String[]{name});
    }

}

